/*******************************************************************************
  * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
  * @file     gesture.c
  * @author   UartTFT Application Team
  * @version  V0.0.1
  * @date     2023-01-04
  * @brief    This file contains the functions prototypes for the UartTFT
  *           Application display firmware.
 ********************************************************************************/

#include "gesture.h"
#include "bsp.h"
#include "stdlib.h"

extern uint8_t Basci_flag;
extern uint8_t Adj_flag;
extern uint8_t Slide_flag;
extern uint8_t Dio_flag;
extern uint8_t AioKb_flag;
extern uint8_t GBKioKb_flag;
extern uint8_t Kb_flag;
extern uint8_t VarKey_flag;
extern uint8_t Pop_up_press;
extern TP_INFO gTpInfo;
extern uint8_t button_Press_flag;
extern void LT_SendData_CRC_Frame(uint8_t *buf, uint8_t len);

#define GESTURE_THRESHOLD 50	  // Gesture effective sliding threshold

uint8_t Gesture_press = 0;		  // Gesture effective press/release
uint8_t Gesture_direction = 0xFF; // Gesture direction
uint8_t Gesture_flag = 0;		  // Gesture function on/off
/*sliding gesture*/
Gesture_Info gGesture_Info;
/***********************************************************************************
 * Function Name : Gesture_touch
 * Description   : Touch detection of gesture.
 * Input         : None
 * Output        : None
 * Return        : None
 ************************************************************************************/
void Gesture_touch(void)
{
	uint8_t i = 0;
	int16_t Gesture_X, Gesture_Y;
	uint8_t rebuf[20];

	// press down
//	if (gTpInfo.sta && button_Press_flag==0)
	if (gTpInfo.sta && Gesture_press==0)
	{
		// sustain
		Gesture_X = gTpInfo.x[0] - First_pressX;
		Gesture_Y = gTpInfo.y[0] - First_pressY;
		if (Gesture_X >= 25 || Gesture_X <= -25 || Gesture_Y >= 25 || Gesture_Y <= -25)
		{
			Gesture_press = 1;

			// cancel
			Basci_flag = 0;
			Adj_flag = 0;
			Slide_flag = 0;
			Dio_flag = 0;
			AioKb_flag = 0;
			GBKioKb_flag = 0;
			Kb_flag = 0;
//			sldmenu_flag = 0;
			VarKey_flag = 0;
			Pop_up_press = 0;
//			button_Press_flag = 1;
		}
	}


	// raise
	if (gTpInfo.sta == 0 && Gesture_press == 1)
	{
		Gesture_press = 0;
		button_Press_flag = 0;
		Gesture_X = gTpInfo.x[0] - First_pressX;
		Gesture_Y = gTpInfo.y[0] - First_pressY;
		if (Gesture_X >= GESTURE_THRESHOLD || Gesture_X <= -GESTURE_THRESHOLD || Gesture_Y >= GESTURE_THRESHOLD || Gesture_Y <= -GESTURE_THRESHOLD)
		{
			if (abs(Gesture_Y) > abs(Gesture_X))
			{
				if (Gesture_Y < 0)
					Gesture_direction = 0; // up
				else
					Gesture_direction = 1; // down
			}
			else
			{
				if (Gesture_X < 0)
					Gesture_direction = 2; // left
				else
					Gesture_direction = 3; // right
			}
		}

		if (Gesture_direction != 0xFF)
		{
			if (gGesture_Info.page[Gesture_direction] != 0xFFFF)
			{
				Gesture_flag = 0;
				if (gGesture_Info.Code == 0xC000)
				{
					rebuf[3] = 0x41;
					rebuf[4] = 0xFF;
					rebuf[5] = 0xFF;
					rebuf[6] = (gGesture_Info.value[Gesture_direction] >> 8) & 0xFF;
					rebuf[7] = gGesture_Info.value[Gesture_direction] & 0xFF;
					LT_SendData_CRC_Frame(rebuf, 5);
				}
				Display_page(gGesture_Info.page[Gesture_direction]);
			}
			Gesture_direction = 0xFF;
		}
	}
}
